package com.big.patient.service.impl;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.big.cmn.DictFeignClient;
import com.big.common.utils.AddressUtil;
import com.big.common.utils.JsonUtils;
import com.big.common.utils.UUIDGenerator;
import com.big.model.model.patient.address.PatientAddress;
import com.big.model.vo.patient.PatientAddressVo;
import com.big.patient.mapper.PatientAddressMapper;
import com.big.patient.service.PatientAddressService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j

public class PatientAddressServiceImpl extends ServiceImpl<PatientAddressMapper, PatientAddress> implements PatientAddressService {


    @Autowired
    DictFeignClient dictFeignClient;

    /**
     * 地址列表
     * @param pId
     * @return
     */
    @Override
    public List<PatientAddress> patientAddressInfos(String pId) {
        QueryWrapper<PatientAddress> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(pId)){
         queryWrapper.eq("p_id",pId);
        }
        List<PatientAddress> patientAddresses = baseMapper.selectList(queryWrapper);
        patientAddresses.forEach(item -> {
          this.addressCodes(item);
        });
        return patientAddresses;
    }

    /**
     * 添加地址
     * @return
     */
    @Override
    public Integer savePatientAddress(PatientAddressVo patientAddressVo) {
        System.out.println(patientAddressVo);
        String[] mallCatergoryCode = patientAddressVo.getMallCatergoryCode();
        //省
        String provinceCode =mallCatergoryCode[0];
        //市
        String cityCode =mallCatergoryCode[1];
        //区
        String districtCode =mallCatergoryCode[2];

        PatientAddress patientAd=new PatientAddress();
        //填入实体类
        patientAd.setPId(patientAddressVo.getPid());
        patientAd.setContactsName(patientAddressVo.getSavename());
        patientAd.setAddress(patientAddressVo.getSavedetailed());
        patientAd.setContactsPhone(patientAddressVo.getSavephone());

        patientAd.setProvinceCode(Integer.valueOf(provinceCode));
        patientAd.setCityCode(Integer.valueOf(cityCode));
        patientAd.setDistrictCode(Integer.valueOf(districtCode));

        patientAd.setId(UUIDGenerator.getUUID7());

        int insert = baseMapper.insert(patientAd);

        return insert;
    }

    /**
     * 查询地址
     * @param Id
     * @return
     */
    @Override
    public PatientAddress PatientAddressInfo(Long Id) {
        QueryWrapper<PatientAddress> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(String.valueOf(Id))){
            queryWrapper.eq("id",Id);
        }
        PatientAddress patientAddresse = baseMapper.selectOne(queryWrapper);
        return patientAddresse;
    }

    //查询ip字典地区
    @Override
    public Map<String, String> dictAddressIp(String address) {
        //TODO 查找字典,截取前台ip信息字符串
        Map<String, String> addressInfo = AddressUtil.getAddressInfo(address);
        return addressInfo;
    }

   //地区数据字典
   public  PatientAddress  addressCodes(PatientAddress patientAddress){

       String provinceName = dictFeignClient.getName(String.valueOf(patientAddress.getProvinceCode()));
       String CityName = dictFeignClient.getName(String.valueOf(patientAddress.getCityCode()));
       String DistrictName = dictFeignClient.getName(String.valueOf(patientAddress.getDistrictCode()));

       patientAddress.getParam().put("provinceName",provinceName);
       patientAddress.getParam().put("CityName",CityName);
       patientAddress.getParam().put("DistrictName",DistrictName);

       return patientAddress;

   }


}
